Skip to content

Conversation

@hadongun
Copy link
Collaborator

πŸ”— 문제 링크

https://www.acmicpc.net/problem/1758

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

20λΆ„

✨ μˆ˜λ„ μ½”λ“œ

μ‹œν—˜ 기간이기도 ν•˜κ³  μƒˆλ‘œμš΄ κ°œλ…μ„ κ³΅λΆ€ν•˜κΈ°μ—” μ‹œκ°„μ΄ λ„‰λ„‰ν•˜μ§€ μ•Šμ•„μ„œ μ΄ˆλ°˜μ— ν–ˆλ˜ 그리디λ₯Ό κ°€μ Έμ™”μŠ΅λ‹ˆλ‹΅

λ¬Έμ œμ—μ„œ κ°•ν˜Έκ°€ λ°›λŠ” 팁의 μ΅œλŒ“κ°’μ„ κ΅¬ν•΄μ•Όν•©λ‹ˆλ‹€.
μ†λ‹˜λ“€μ΄ κ°•ν˜Έμ—κ²Œ μ£ΌλŠ” νŒμ€ μ›λž˜ 주렀던 팁 - (컀피λ₯Ό 받은 μˆœμ„œ - 1) μž…λ‹ˆλ‹€.
μœ„ 식을 톡해 κ³„μ‚°λœ 값이 음수라면 μ†λ‹˜λ“€μ€ νŒμ„ μ£Όμ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

전체 μ†λ‹˜μ˜ μˆ˜μ™€ μ›λž˜ μ†λ‹˜λ“€μ΄ 주렀던 νŒμ€ μ²˜μŒμ— μ£Όμ–΄μ§€κ³  λ°”λ€Œμ§€ μ•ŠμŠ΅λ‹ˆλ‹€. 그럼 팁의 μ΅œλŒ“κ°’μ„ κ΅¬ν•˜κΈ° μœ„ν•œ 핡심은
μ›λž˜ 주렀던 팁 - (컀피λ₯Ό 받은 μˆœμ„œ - 1) < 0 이 λΆ€λΆ„μž…λ‹ˆλ‹€. μ›λž˜ 주렀던 팁이 큰 μ†λ‹˜μ΄ κ³„μ†ν•΄μ„œ 기닀리닀 κ²°κ΅­ νŒμ„ λͺ» 주게 λœλ‹€λ©΄ 손싀이 κ°€μž₯ 크게 λ©λ‹ˆλ‹€.

λ”°λΌμ„œ μ†λ‹˜λ“€μ΄ μ›λž˜ 주렀던 νŒμ„ κΈ°μ€€μœΌλ‘œ λ‚΄λ¦Όμ°¨μˆœμœΌλ‘œ μ •λ ¬ν•˜μ—¬ 쀄 μ„Έμ›Œ 팁의 합을 κ΅¬ν•˜λ©΄ μ΅œλŒ“κ°’μ΄ λ©λ‹ˆλ‹€.?.λ„΅

μˆ˜λ„μ½”λ“œ

μž…λ ₯: μ†λ‹˜ 수 N
μž…λ ₯: Nλͺ…μ˜ κΈ°λŒ€ 팁 리슀트 tip[]

1. tip 리슀트λ₯Ό λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬ν•œλ‹€ (팁이 큰 μ†λ‹˜ λ¨Όμ € 처리)
2. receive = 0  (받을 수 μžˆλŠ” 총 팁)

3. for i in 0λΆ€ν„° N-1κΉŒμ§€:
      μ‹€μ œ 팁 = tip[i] - i   // iλŠ” ν˜„μž¬ μ†λ‹˜μ„ μ²˜λ¦¬ν•œ μˆœμ„œ
      λ§Œμ•½ μ‹€μ œ 팁 < 0이면:
          continue  // 0원 μ΄ν•˜λ‘œ 쀄 순 μ—†μœΌλ‹ˆ κ±΄λ„ˆλœ€
      receive += μ‹€μ œ 팁

4. receive 좜λ ₯

## πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš© λ³€μˆ˜.sort(reverse=True)λ₯Ό μ‚¬μš©ν•˜λ©΄ 손 μ‰½κ²Œ λ‚΄λ¦Όμ°¨μˆœ 정렬을 μˆ˜ν–‰ν•  수 μžˆλ‹€.(좜처 = μ±—μ§€ν”Όν‹°)

Copy link
Contributor

@dohyeondol1 dohyeondol1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

κ°„λ‹¨ν•œ 그리디 λ¬Έμ œλ„€μš”!
팁이 큰 순으둜 μ •λ ¬ν•΄μ„œ μ΅œλŒ“κ°’μ„ λ„μΆœν•΄λƒˆμŠ΅λ‹ˆλ‹€.

C++ μ½”λ“œ
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int N;
    cin >> N;
    
    vector<int> tip(N);
    for(int& t : tip)
        cin >> t;
    
    sort(tip.rbegin(), tip.rend());
    
    long long totalTip = 0;
    for(int i = 0; i < N; i++) {
        if(tip[i]-i < 0) continue;
        totalTip += tip[i]-i;
    }
    
    cout << totalTip << '\n';
    
    return 0;
}

μ‹œν—˜κ³΅λΆ€ νŒŒμ΄νŒ…~

@froglike6
Copy link
Collaborator

저도 reversed=Trueλ₯Ό 톡해 μ‰½κ²Œ 그리디 문제λ₯Ό ν’€μ–΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€.
κ°„λ‹¨ν•œ 문제라 λ‹€λ₯Έ 뢀뢄은 μž…λ ₯λ°›λŠ” 뢀뢄밖에 μ—†λŠ” 것 κ°™λ„€μš”

파이썬3

import sys
input = sys.stdin.readline
N = int(input())
tips=[]
for _ in range(N):
    tips.append(int(input()))
tips.sort(reverse=True)
total_tip = 0
for i in range(N):
    actual_tip = tips[i] - i
    if actual_tip > 0:
        total_tip += actual_tip
print(total_tip)

Copy link
Collaborator

@caucsejunseo caucsejunseo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ΄λ²ˆμ— 배운 μ‚½μž…μ •λ ¬μ„ μ΄μš©ν•΄μ„œ ν’€μ–΄λ΄€μŠ΅λ‹ˆλ‹€! 백쀀에선 μ‹œκ°„μ΄ˆκ³Όκ°€ λœ¨λ„€μš”.
λ°°μš΄λŒ€λ‘œ μ‹œκ°„λ³΅μž‘λ„λ₯Ό 계산해보면 ν™•μ‹€νžˆ 느리긴 ν•©λ‹ˆλ‹€.

C μ½”λ“œ

#include <stdio.h>
#define MAX 100002

void insertion_sort(int tiparr[], int tip, int count);

int main(void) {

    int N = 0;
   
    scanf("%d", &N);
    long long sum = 0;
    int tiparr[MAX] = { 0 };
    int count = 0;
    
    for (int i = 0; i < N; i++)
    {
        int tip = 0;
        scanf("%d", &tip);

        
        insertion_sort(tiparr, tip, count);

        count++;
    }
    for (int i = 0; i < N ; i++)
    {
        if (tiparr[i] - i <= 0)
        {
            break;
        }
        else if(tiparr[i] -i > 0)
        {
            sum += tiparr[i] - i;
        }
    }

    printf("%lld", sum);

    return 0;
}

void insertion_sort(int tiparr[], int tip, int count)
{
    tiparr[count] = tip;
    int j = count;
    
    while (j>0&&tiparr[j-1] < tiparr[j])
    {
        int temp = tiparr[j - 1];
        tiparr[j - 1] = tiparr[j];
        tiparr[j] = temp;
        j--;
    }
    
 
}```

@hadongun hadongun merged commit 6e8eb72 into AlgoLeadMe:main May 2, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants